package com.baize.baizeapp.test.video

import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.baize.baizeapp.R
import com.baize.baizeapp.test.MohuActivity
import com.baize.baizeapp.utils.SPUtils


class VideoActivity : AppCompatActivity() {

    companion object {
        fun open(context: Context) {
            val intent = Intent(context, VideoActivity::class.java)
            context.startActivity(intent)
        }
    }

    lateinit var videoView: HeiShiTTVideo

    val videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
    val videoFile = "/storage/emulated/0/Pictures/WeiXin/wx_camera_1619238338335.mp4"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_video)
        videoView = findViewById(R.id.textureView)
        videoView.setUrl(videoUrl)
//        videoView.setPath(videoFile)
        videoView.setComponentListener(object : TTVideoComposeEventListener {
            //视频单击事件，返回值：是否继续触发默认事件（播放，暂停）
            override fun onVideoClickListener(): Boolean {
                Toast.makeText(this@VideoActivity,"自定义点击",Toast.LENGTH_SHORT).show()
                return true
            }

            //是否缓存上次播放时间（内存缓存）
            override fun isNeedCachePlayTime(): Boolean {
                return true
            }

            //展示横竖屏切换按钮
            override fun onChangeScreenButtonShow(): Boolean {
                return true
            }
        })
//        videoView.play()

        val md5Str = HeiShiTTVideoHelper.md5("123456")
        Log.i("yanze_password","md5:${md5Str}")
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setFullScreen(false)
        } else {
            setFullScreen(true)
        }
    }

    fun setFullScreen(fullScreen: Boolean) {
        val attrs = getWindow().getAttributes()
        if (fullScreen) {
            attrs.flags = attrs.flags or WindowManager.LayoutParams.FLAG_FULLSCREEN
            getWindow().setAttributes(attrs);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        } else {
            attrs.flags = attrs.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN.inv()
            getWindow().setAttributes(attrs)
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        }
    }

    fun play(v: View) {
        videoView.setUrl(videoUrl)
        videoView.play()
    }

    fun pause(v: View) {
        videoView.pause()
    }

    fun stop(v: View) {
        videoView.stop()
    }

    fun newActivity(v: View) {
        PreVideoActivity.open(this)
//        videoView.release()
//        videoView.stopProcessRefresh()
//        videoView.ttVideoEngine?.release()
    }

    override fun onBackPressed() {
        if (videoView.backPressed()) {
            super.onBackPressed()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        videoView.release()
    }

}